<?php
/*
*功能：数据库操作函数
*@autor likun
*@email //////
*@param $host  string
*@param $user  string
*@........
*/

function connect($host , $user , $pwd , $charset , $dbName)
{

	$link = mysqli_connect($host , $user , $pwd);
	//var_dump($link);
	if (!$link) {
		return false;
	}
	
	mysqli_set_charset($link , $charset);
	//var_dump(mysqli_set_charset($link , $charset));
	if (!mysqli_select_db($link , $dbName)) {
		return false;
	}
	//var_dump($link);
	return $link;
	
}

//$link = connect('localhost','root' ,'','utf8','dingjiangbbs');
//var_dump($link);


/*
$time = time();

$sql = "insert into bbs_user(username , password , email ,regtime , regip , realname , sex  , birthday , place , qq) values('张三' , md5('123') , 'sdfs@qq.com' , $time,168508717,'zhenshi' , 1 , 'wqweqwe' , 'dizhi' , 123)";
echo $sql;
mysqli_query($link , $sql);
*/
//封装一个插入的方式


function insert($link , $table , $data)
{
	
	//var_dump($data);
	
	/*
	array (size=4)
  'username' => string '用户名' (length=9)
  'password' => string '123123123' (length=9)
  'sex' => string '1' (length=1)
  'ctime' => int 1494830360
  */
  
  $keys = array_keys($data);
  //处理字段问题
  $fields = join(',' , $keys);
  
  //var_dump($fields);
  
  $value = array_values($data);
  
  $values = implode(',' , parseValue($value));
  
 // var_dump($values);
	

 
  //var_dump($keys);  // inser into bbs_user(username ,password , sex ,ctime) values('姓名' ， '密码' ， '性别' ， 时间)；
  
  $sql = "insert into $table($fields) values($values)";

  //var_dump($sql);
  $result = mysqli_query($link , $sql);
  //var_dump(mysqli_error($link));
  //var_dump($result);
  if ($result) {
	  return mysqli_insert_id($link);
  } else {
	  return false;
  }
  
	
}




//处理数据值函数
function parseValue($data)
{
	
	//var_dump($data);
	if (is_string($data)) {
		$data = '\''.$data.'\'';
	} else if (is_array($data)) {
		$data = array_map('parseValue' , $data);
	} else if(is_null($data)){
		$data = null;
	}
	
	//var_dump($data);
	return $data;
	
}

//封装修改方式
function update($link , $table , $data , $where)
{
	//$sql = "update user set username=“女人” ， password=“” ,sex =  '' where id>=10";
	
	//var_dump($data);
	
	$set = join(',' , parseSet($data));

	//var_dump($set);
	
	$sql = "update $table set $set where $where ";
	//var_dump($sql);
	
	$result = mysqli_query($link , $sql);
	//var_dump(mysqli_error($link));	//打印错误日志
	//var_dump($result);
	
		return $result;
	
	
}

//处理修改字段
function parseSet($data)
{
	//var_dump($data);
	
	foreach ($data as $key=>$val) {
		$value = parseValue($val);
		//var_dump($value);
		if (is_scalar($value)) {
			$set[] = $key . '=' . $value;
			//var_dump($set);
		}
		
	}
	//var_dump($set);
	return $set;
	
}

//查询方法
function select($link , $table , $fields , $where='')
{

	$sql = "select $fields from $table  $where";
		//var_dump($sql);

	$result = mysqli_query($link , $sql);
		//var_dump($result);
		//var_dump(mysqli_error($link));
	if ($result && mysqli_affected_rows($link)) {
		while ($row = mysqli_fetch_assoc($result)) {
			$data[] = $row;
		}
		//var_dump($data);
		return $data;

	} else {
		//查找的字段为空时走此区间
		//echo '<br />11111111111111111111111<br />';
		return false;
	}
	
}
//var_dump($link);
//$res = select($link,'bbs_link','name','lid=1');

//var_dump($res);

//删除的方法
function del($link , $table , $where)
{
	$sql = "delete from $table where $where";
	//var_dump($sql);
	$result = mysqli_query($link , $sql);
	
	if ($result && mysqli_affected_rows($link)) {
		return mysqli_affected_rows($link);
	} else {
		return false;
	}
}

//求和方法
function sum($link , $table , $fields)
{
	$sql = "select sum($fields) as sum from $table";
		//var_dump($sql);
	$result = mysqli_query($link , $sql);
		//var_dump($result);
	$sum = mysqli_fetch_assoc($result);
		//var_dump($sum);
	return $sum['sum'];
	
}

//求总条数
function idCount($link , $table , $fields)
{
	$sql = "select count($fields) as count from $table";
	//var_dump($sql);
	$result = mysqli_query($link , $sql);
	//var_dump($result);
	$count = mysqli_fetch_assoc($result);
	//var_dump($count);
	return $count['count'];
	
}


	
//查询方法333333333333
function select333($link , $table , $fields , $where='')
{

	$sql = "select $fields from $table  $where";
		var_dump($sql);

	$result = mysqli_query($link , $sql);
	return mysqli_fetch_assoc($result);
	
}